package com.itheima.controller.backend.Service.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.DTO.CheckItemDTO;
import com.itheima.VO.CheckItemVO;
import com.itheima.common.entity.PageResult;
import com.itheima.controller.backend.Service.CheckItemService;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void addCheckItem(CheckItem checkItem){
        String sex = checkItem.getSex();
        if ("不限".equals(sex)){
            checkItem.setSex("0");
        }
        else if ("男".equals(sex)){
            checkItem.setSex("1");
        }
        else if ("女".equals(sex)){
            checkItem.setSex("2");
        }
        String type = checkItem.getType();
        checkItem.setType(type.equals("检查")?"1":"2");
        checkItemMapper.addCheckItem(checkItem);
    }

    @Override
    public PageResult findPage(CheckItemDTO checkItemDTO, Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<CheckItemVO> checkItemList = checkItemMapper.findPage(checkItemDTO);
        Page<CheckItemVO> checkItems = (Page) checkItemList;
        return new PageResult(checkItems.getTotal(),checkItems.getResult());
    }

    @Override
    public void deleteCheckItem(Integer id){
        checkItemMapper.deleteCheckItem(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id){
        return checkItemMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckItem checkItem){
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll(){
        return checkItemMapper.findAll();
    }

    @Override
    public CheckItem findById(Integer id){
        return checkItemMapper.findById(id);
    }
}
